from typing import List
from common.enum.strategy_group_enum import StrategyGroup
from common.enum.strategy_type_enum import StrategyType
from dto.strategy_info import StrategyInfoMetadata
from dto.strategy_assessment import RiskAssessment
from dto.strategy_stock_day import StrategyStockDay
from service.strategy.base_strategy import BaseStrategy


class FifteenDaysAttentionStrategy(BaseStrategy):
    """
    15天内上涨占比策略，判断过去15天中上涨天数是否占一半以上，
    且最新价格对比最低点上涨了10%以上，每多一天上涨额外加1分。
    """

    def analyze(self, trade_info_list: List[StrategyStockDay]) -> RiskAssessment:
        """
        分析过去15天的交易数据，计算得分。
        """
        node_point = 0
        description = "未满足策略条件"

        # 确保至少有15天的交易数据
        if len(trade_info_list) >= 15:
            # 统计上涨天数
            up_days = sum(1 for day in trade_info_list if day.close > day.open)

            # 获取最低点和最新价格
            lowest_price = min(day.low for day in trade_info_list)
            latest_price = trade_info_list[-1].close

            # 检查条件：上涨天数 >= 一半，且最新价格较最低点涨幅 >= 10%
            if up_days >= 8 and (latest_price - lowest_price) / lowest_price >= 0.1:
                # 基础得分为 5，每多一天上涨加1分
                node_point = 5 + (up_days - 8)
                description = f"过去15天内上涨天数为 {up_days} 天，且最新价格较最低点上涨超过10%，符合策略条件。"

        # 创建策略信息对象
        return RiskAssessment(
            stock_code=trade_info_list[0].stock_code,
            description=description,
            config=self.strategyConfig(),
            node_point=node_point,
        )

    def strategyConfig(self) -> StrategyInfoMetadata:
        """
        返回策略的配置信息。
        """
        return StrategyInfoMetadata(
            strategy_code="fifteen_days_attention",
            strategy_name="15天内上涨占比策略",
            strategy_group=StrategyGroup.ATTENTION,
            strategy_type=StrategyType.PRICE,
            analysis_day=15,
            strategy_level=3,
        )
